import { useStore } from "../store"

/**
 * 检测 token 是否存在，以及token是否有效
*/
function checkAuth(token) {
  const { userStore } = useStore()
	const token = userStore.token
	
	if (!token) return false
	return true
}

// 需要登录的页面路径
export const authPages = [
  '/pages/user/user',
  '/pages/order/order',
]

/**
 * 给 uniapp 提供的页面跳转方法添加拦截器
*/
function setNavMethodsInterceptor() {
	const navMethods = ['navigateTo', 'redirectTo', 'switchTab', 'reLaunch']
	const { userStore } = useStore()
	
	navMethods.forEach((method) => {
		uni.addInterceptor(method, {
			invoke: (args) => {
				const needAuth = authPages.some(page => args.url.startsWith(page))
				if (!needAuth) return true
				
				return checkAuth()
			}
		})
	})
}
